package com.tubruk.adapter;

import java.util.ArrayList;

import com.tubruk.database.DatabaseHelper;
import com.tubruk.database.OptionDB;
import com.tubruk.object.ConstantList;
import com.tubruk.object.IOption;
import com.tubruk.object.Option;
import com.tubruk.object.option.ListOption;
import com.tubruk.trackme.R;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class OptionAdapter extends ArrayAdapter<IOption> {
	private Context context;
	
	private ArrayList<IOption> items;
	private LayoutInflater vi;
	
	public OptionAdapter(Context context, int textViewResourceId, ArrayList<IOption> items) {
        super(context, textViewResourceId, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
	
		IOption option = items.get(position);
		if (option.getMode() == DatabaseHelper.TABLE_CONSTANT.MODE_TITLE){
			v = vi.inflate(R.layout.option_title, null);
			
			TextView tv = (TextView) v.findViewById(R.id.title);
			tv.setText(option.getTitle());
			
		}else if (option.getMode() == DatabaseHelper.TABLE_CONSTANT.MODE_FREETEXT_INTEGER){
			
			
			v = vi.inflate(R.layout.option_editint, null);
			
			TextView tv = (TextView) v.findViewById(R.id.title);
			tv.setText(option.getTitle());
			
			TextView tvDesc = (TextView) v.findViewById(R.id.description);
			tvDesc.setText(option.getDescription());
			
			EditText et = (EditText) v.findViewById(R.id.edit);
			
			et.setText(option.getValue());
			et.addTextChangedListener(new OptionTextWatcher((Option) option));
			
		}else if (option.getMode() == DatabaseHelper.TABLE_CONSTANT.MODE_LIST){
			
			ListOption convertedOption = (ListOption) option;
			
			v = vi.inflate(R.layout.option_list, null);
			
			TextView tv = (TextView) v.findViewById(R.id.title);
			tv.setText(convertedOption.getTitle());
		
			TextView tvDesc = (TextView) v.findViewById(R.id.description);
			tvDesc.setText(option.getDescription());
			
			ConstantListAdapter adapter = new ConstantListAdapter(this.context, R.layout.constantlist, convertedOption.getAllowableValues());
			
			Spinner spn = (Spinner) v.findViewById(R.id.spinner);
			spn.setAdapter(adapter);
			
			spn.setSelection(convertedOption.findSelectedIndex());
			
			spn.setOnItemSelectedListener(new OptionListListener(spn, convertedOption));
		}
		
		return v;
	}
	
	private class OptionListListener implements OnItemSelectedListener {
		private ListOption option;
		private Spinner spn;
		
		public OptionListListener(Spinner spn, ListOption option){
			this.spn = spn;
			this.option = option;
		}

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			
			ConstantList selected = (ConstantList) spn.getSelectedItem();
			this.option.setValue(Integer.toString( selected.getId() ));
			
			OptionDB.updateValueByPK(option);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class OptionTextWatcher implements TextWatcher {
		private Option option;
		
		public OptionTextWatcher(Option option){
			this.option = option;
		}
		
		public void afterTextChanged(Editable arg0) {
			option.setValue(arg0.toString());
			OptionDB.updateValueByPK(option);
		}

		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			
		}
		
	}
	
	
}
